package com.handpay.zztong.hp;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.handpay.framework.ClientEngine;
import com.handpay.framework.MD5;
import com.handpay.framework.SecureManager;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.bean.QrCodeBean;
import com.handpay.zztong.hp.bean.ResponseBean;
import com.handpay.zztong.hp.config.ZZTConfig;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.protocol.ATProtocolConstant;
import com.handpay.zztong.hp.protocol.BaseProtocolKey;
import com.handpay.zztong.hp.trans.activity.VPOSActivity;
import com.handpay.zztong.hp.utils.AccountUtils;
import com.xys.libzxing.zxing.encoding.EncodingUtils;

import java.math.BigDecimal;
import java.util.Hashtable;

import static com.handpay.zztong.hp.R.string.capture_title_merchant;

/**
 * @author sxshi
 * @describe:新版秒秒通收款二维码收款界面
 */
public class QRCodeReceiptActivity extends ZZTongActivity implements OnClickListener {
    private ImageView qrcode_iv;
    private Bitmap mQRCodeBitmap;
    private double amount;//支付金额
    private TextView amount_tv;
    private LinearLayout root_ll;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.setContentView(R.layout.activity_qrcode_receipt);
        super.onCreate(savedInstanceState);
        int qrcodeType=getIntent().getIntExtra("qrcodeType",-1);
        HPLog.i(HPLog.LFP_TAG,"qrcodeType:"+qrcodeType);
        root_ll=(LinearLayout)findViewById(R.id.root_ll);
        ImageView icon_pay_channel_iv=(ImageView)findViewById(R.id.icon_pay_channel_iv);
        TextView qr_code_tips_tv=(TextView)findViewById(R.id.qr_code_tips_tv);
        qrcode_iv = (ImageView) this.findViewById(R.id.qrcode_iv);
        amount_tv = (TextView) findViewById(R.id.tv_amount);
        if(qrcodeType==4){
            myActionBar.refreshActionBar(new ActionBar(this.getString(R.string.wechatpay_qr_code), true));
            myActionBar.setBackgroundColor(Color.TRANSPARENT);
            root_ll.setBackgroundResource(R.drawable.qr_code_wechat_pay_bg);
            icon_pay_channel_iv.setBackgroundResource(R.drawable.home_icon_wechatpay);
            qr_code_tips_tv.setText(R.string.wechatpay_qr_code_tips);
        }else if(qrcodeType==8){
            myActionBar.refreshActionBar(new ActionBar(this.getString(R.string.alipay_qr_code), true));
            myActionBar.setBackgroundColor(Color.TRANSPARENT);
            root_ll.setBackgroundResource(R.drawable.qr_code_alipay_bg);
            icon_pay_channel_iv.setBackgroundResource(R.drawable.home_icon_alipay);
            qr_code_tips_tv.setText(R.string.alipay_qr_code_tips);
        }
        amount = this.getIntent().getDoubleExtra(VPOSActivity.Transfer_Amount_Key, 0d);
        if(qrcodeType!=-1){
            getTransferQrCode(qrcodeType);
        }else{
           finish();
        }

        String amountStr = String.format("%s", CommonUtils.formatMoneyWithSplite(amount/100));
//        SpannableStringBuilder ssb = new SpannableStringBuilder(amountStr);
//        ssb.setSpan(new RelativeSizeSpan(1.5f), 1, ssb.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        amount_tv.setText(amountStr);
    }

    private void getTransferQrCode(int tradeType){
        Hashtable<String, String> params = new Hashtable<String, String>();
        params.put("amount", SecureManager.getInstance().des(BigDecimal.valueOf(amount).toPlainString(), 1, null));
        params.put(BaseProtocolKey.CHANNEL, ClientEngine.CHANNEL);
        params.put(ATProtocolConstant.CLIENTCHANNEL, ZZTConfig.UPDATE_CHANNEL);
        params.put("tradeType", tradeType + "");
        params.put("clientVersion", "v" + ClientEngine.VERSION);
        Double latitude = (Double) ClientEngine.getInstance().getGlobal(BaseProtocolKey.KEY_LATITUDE, 0d);
        Double longtitude = (Double) ClientEngine.getInstance().getGlobal(BaseProtocolKey.KEY_LONGTITUDE, 0d);
        String address = (String) ClientEngine.getInstance().getGlobal(BaseProtocolKey.KEY_ADDRESS);
        params.put(BaseProtocolKey.KEY_LOCATION, latitude.toString() + "," + longtitude.toString());
        params.put(BaseProtocolKey.KEY_ADDRESS, address);
        HPLog.i(HPLog.LFP_TAG,"getTransferQrCode::params:"+params.toString());
        this.doJsonPost(true,this,ZZTongActivity.Do_QrCodeTransfer, QrCodeBean.class,params,true);
    }

    @Override
    public boolean netJsonResponse(String action, ResponseBean resp, boolean showAlert, DialogInterface.OnClickListener on) {
        if (super.netJsonResponse(action, resp, showAlert, on)) {
            return true;
        }
        if (ZZTongActivity.Do_QrCodeTransfer.equals(action)) {
            QrCodeBean qrCodeBean=(QrCodeBean)resp;
            if(qrCodeBean!=null){
                String mQrCodeUrl = SecureManager.getInstance().des(qrCodeBean.getQrCodeUrl(), 0, null);
                showQrCodeInfo(mQrCodeUrl);
            }
        }
        return false;
    }

    private void showQrCodeInfo(String url){
        BitmapDrawable ic_launcher = (BitmapDrawable) this.getResources().getDrawable(R.drawable.ic_launcher);
        mQRCodeBitmap = EncodingUtils.createQRCode(url, CommonUtils.dp2px(300), CommonUtils.dp2px(300), ic_launcher.getBitmap());
        qrcode_iv.setImageBitmap(mQRCodeBitmap);
    }


    @Override
    protected boolean isShownTopBar() {
        return true;
    }

    @Override
    public ActionBar getActivityActionBar() {
        return new ActionBar(this.getString(R.string.capture_title_merchant), true);
    }

}
